<template>
  <div class="page-member-manage-list mod-user">
    <div class="search-bar">
      <el-form
        ref="searchFormRef"
        :inline="true"
        class="search-form"
        :model="searchForm"
        @submit.prevent
      >
        <div class="input-row">
          <el-form-item
            prop="nickName"
            :label="$t('users.name') + ':'"
          >
            <el-input
              v-model="searchForm.nickName"
              type="text"
              clearable
              :placeholder="$t('users.name')"
            />
          </el-form-item>
          <el-form-item
            prop="userMobile"
            :label="$t('publics.mobilePhone') + ':'"
          >
            <el-input
              v-model="searchForm.userMobile"
              type="text"
              clearable
              maxlength="11"
              :placeholder="$t('publics.mobilePhone')"
            />
          </el-form-item>
          <el-form-item
            prop="auditStatus"
            label="审核状态:"
          >
            <el-select
              v-model="searchForm.auditStatus"
              clearable
              placeholder="审核状态"
            >
              <el-option
                v-for="item in auditStatusList"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item
            prop="auditResult"
            label="审核结果:"
          >
            <el-select
              v-model="searchForm.auditResult"
              clearable
              :placeholder="$t('publics.status')"
            >
              <el-option
                v-for="item in auditResultList"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>

          <el-form-item>
            <div
              class="default-btn primary-btn"
              @click="onSearch(true)"
            >
              {{ $t('crud.searchBtn') }}
            </div>
            <div
              class="default-btn"
              @click="resetForm()"
            >
              {{ $t('product.reset') }}
            </div>
          </el-form-item>
        </div>
      </el-form>
    </div>

    <div class="main-container">
      <div class="table-con">
        <div
          v-if="dataList.length === 0"
          class="empty-text"
        >
          {{ $t('user.noData') }}
        </div>
        <el-table
          :data="dataList"
          header-cell-class-name="table-header"
          row-class-name="table-row-low"
          style="width: 100%"
        >
          <el-table-column
            fixed
            :label="$t('users.name')"
            prop="nickName"
            align="left"
            width="120"
          />

          <el-table-column
            prop="userMobile"
            width="120"
            :label="$t('publics.mobilePhone')"
          >
            <template #default="scope">
              {{ isTel(scope.row.userMobile) ? scope.row.userMobile : '--' }}
            </template>
          </el-table-column>

          <el-table-column
            width="120"
            label="微信号"
            show-overflow-tooltip
          >
            <template #default="scope">
              {{ scope.row.unionId || '--' }}
            </template>
          </el-table-column>

          <el-table-column
            width="150"
            label="邮箱"
            show-overflow-tooltip
          >
            <template #default="scope">
              {{ scope.row.userMail || '--' }}
            </template>
          </el-table-column>

          <el-table-column
            width="120"
            label="身份"
          >
            <template #default="scope">
              {{ scope.row.identity || '--' }}
            </template>
          </el-table-column>

          <el-table-column
            width="120"
            label="真实姓名"
          >
            <template #default="scope">
              {{ scope.row.realName || '--' }}
            </template>
          </el-table-column>

          <el-table-column
            width="120"
            label="公司"
            show-overflow-tooltip
          >
            <template #default="scope">
              {{ scope.row.company || '--' }}
            </template>
          </el-table-column>

          <el-table-column
            width="120"
            label="职位"
          >
            <template #default="scope">
              {{ scope.row.position || '--' }}
            </template>
          </el-table-column>

          <el-table-column
            width="120"
            label="工作证照"
          >
            <template #default="scope">
              <el-image
                v-if="!!scope.row.workPermit"
                class="tab-img"
                :src="checkFileUrl(scope.row.workPermit)"
                fit="cover"
                preview-teleported
                :preview-src-list="[checkFileUrl(scope.row.workPermit)]"
              />
              <span v-else>--</span>
            </template>
          </el-table-column>

          <el-table-column
            width="120"
            label="申请等级"
          >
            <template #default="scope">
              {{ memberTypeList.find(d => d.value == scope.row.memberType)?.label || '--' }}
            </template>
          </el-table-column>

          <el-table-column
            width="180"
            label="申请时间"
          >
            <template #default="scope">
              {{ scope.row.applyTime || '--' }}
            </template>
          </el-table-column>

          <el-table-column
            width="120"
            label="审核状态"
          >
            <template #default="scope">
              {{ auditStatusList.find(d => d.value == scope.row.auditStatus)?.label || '--' }}
            </template>
          </el-table-column>

          <el-table-column
            width="120"
            label="审核结果"
          >
            <template #default="scope">
              {{ auditResultList.find(d => d.value == scope.row.auditResult)?.label || '--' }}
            </template>
          </el-table-column>

          <el-table-column
            width="200"
            label="备注"
            show-overflow-tooltip
          >
            <template #default="scope">
              {{ scope.row.remark || '--' }}
            </template>
          </el-table-column>

          <el-table-column
            fixed="right"
            align="center"
            :label="$t('crud.menu')"
            width="100"
          >
            <template #default="scope">
              <div class="text-btn-con">
                <div
                  v-if="scope.row.auditStatus == 0"
                  class="text-btn default-btn"
                  @click.stop="onApprove(scope.row.id, scope.row.userId)"
                >
                  审核
                </div>
                <div
                  v-if="scope.row.auditStatus == 1"
                  class="default-btn text-btn"
                  @click.stop="onDel(scope.row.id)"
                >
                  删除
                </div>
              </div>
            </template>
          </el-table-column>

          <template #empty>
            <div>
              &nbsp;
            </div>
          </template>
        </el-table>
      </div>
      <el-pagination
        :current-page="page.currentPage"
        :page-sizes="[10, 20, 50, 100]"
        :page-size="page.pageSize"
        :total="page.total"
        layout="total, sizes, prev, pager, next, jumper"
        @size-change="onPageSizeChange"
        @current-change="onPageChange"
      />
    </div>

    <!-- 审核 -->
    <el-dialog
      v-model="visible"
      :close-on-click-modal="false"
      title="审核"
    >
      <el-form
        ref="dataFormRef"
        :model="dataForm"
        :rules="dataRule"
        @submit.prevent
      >
        <el-form-item
          label="审核结果"
          prop="auditResult"
        >
          <el-radio-group
            v-model="dataForm.auditResult"
            @change="onChangeResult"
          >
            <el-radio :label="'1'">
              通过
            </el-radio>
            <el-radio :label="'0'">
              不通过
            </el-radio>
          </el-radio-group>
        </el-form-item>

        <el-form-item
          label="备注"
          prop="remark"
        >
          <el-input
            v-model.trim="dataForm.remark"
            maxlength="100"
            show-word-limit
            type="textarea"
          />
        </el-form-item>
      </el-form>

      <template #footer>
        <div>
          <div
            class="default-btn"
            @click="visible = false"
          >
            {{ $t("coupon.cancel") }}
          </div>
          <div
            class="default-btn primary-btn"
            @click="onSubmitApprove()"
          >
            {{ $t("coupon.confirm") }}
          </div>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { isTel } from '@/utils/validate'
import { ElMessageBox, ElMessage } from 'element-plus'
import { isAuth } from '@/utils'

let tempSearchForm = null // 保存上次点击查询的请求条件
const dataList = ref([])
const page = reactive({
  total: 0, // 总页数
  currentPage: 1, // 当前页数
  pageSize: 10 // 每页显示多少条
})

const searchForm = reactive({
  nickName: '',
  userMobile: '',
  auditStatus: '',
  auditResult: ''
})

const auditStatusList = reactive([
  {
    label: '未审核',
    value: '0'
  },
  {
    label: '已审核',
    value: '1'
  }
])

const auditResultList = reactive([
  {
    label: '未通过',
    value: '0'
  },
  {
    label: '已通过',
    value: '1'
  }
])

const memberTypeList = [
  {
    label: '普通会员',
    value: '0'
  },
  {
    label: '认证会员',
    value: '1'
  },
  {
    label: '推广会员',
    value: '2'
  }
]

const visible = ref(false)

const dataForm = ref({
  id: null,
  userId: null,
  auditResult: '',
  remark: ''
})

const dataRule = ref({
  auditResult: [
    {
      required: true,
      message: '审核结果不能为空',
      trigger: 'blur'
    }
  ],
  remark: [
    {
      required: false,
      message: '备注不能为空',
      trigger: 'blur'
    }
  ]
})

onMounted(() => {
  getDataList(page)
})

// 获取数据列表  /admin/user/page
const getDataList = (pageParam, newData = false) => {
  if (newData || !tempSearchForm) {
    tempSearchForm = JSON.parse(JSON.stringify(searchForm))
  }
  http({
    url: http.adornUrl('/shop/memberAudit/page'),
    method: 'get',
    params: http.adornParams(
      Object.assign(
        {
          current: pageParam == null ? page.currentPage : pageParam.currentPage,
          size: pageParam == null ? page.pageSize : pageParam.pageSize
        },
        tempSearchForm
      )
    )
  }).then(({ data }) => {
    dataList.value = data.records
    page.total = data.total
  })
}

// 审核
const dataFormRef = ref(null)
const onApprove = (id, userId) => {
  visible.value = true
  dataForm.value.id = id
  dataForm.value.userId = userId
  dataForm.value.remark = ''
  dataForm.value.auditResult = ''
}

// 条件查询 JSON.stringify(arr)
const onSearch = (newData = false) => {
  page.currentPage = 1
  getDataList(page, newData)
}

const onPageSizeChange = (val) => {
  page.pageSize = val
  getDataList(page)
}
const onPageChange = (val) => {
  page.currentPage = val
  getDataList(page)
}
const searchFormRef = ref(null)
const resetForm = () => {
  searchFormRef.value.resetFields()
}

const onSubmitApprove = () => {
  dataFormRef.value?.validate((valid) => {
    if (valid) {
      http({
        url: http.adornUrl('/shop/memberAudit'),
        method: 'put',
        data: http.adornData({
          ...dataForm.value,
          auditStatus: '1'
        })
      }).then(() => {
        visible.value = false
        ElMessage.success('操作成功')
        getDataList(page)
      })
    }
  })
}

const onDel = (id) => {
  ElMessageBox.confirm('确认删除此条记录嘛?', $t('resource.tips'), {
    confirmButtonText: $t('resource.confirm'),
    cancelButtonText: $t('resource.cancel'),
    type: 'warning'
  }).then(() => {
    http({
      url: http.adornUrl(`/shop/memberAudit/${id}`),
      method: 'delete'
    }).then(() => {
      ElMessage.success('操作成功')
      getDataList(page)
    })
  })
}

const onChangeResult = (e) => {
  dataRule.value.remark[0].required = e === '0'
}
</script>

<style lang="scss" scoped>
.page-member-manage-list.mod-user {
  :deep(.main-container){
    .el-table__fixed{
      height: auto !important;
      bottom: 16px !important;
      &::before {
        background-color: transparent !important;
      }
    }
    .el-table__fixed-right{
      height: auto !important;
      bottom: 16px !important;
      &::before {
        background-color: transparent !important;
      }
    }
   .export-load {
      top: -50% !important;
    }
    .el-select .el-select__tags > span > .el-tag.el-tag--info:nth-last-child(2) {
      max-width: 120px;
    }
  }

  .table-con {
    position: relative;
  }

  .empty-text {
    position: absolute;
    z-index: 4;
    top: 50%;
    left: 50%;
    color: #909399;
  }

}
</style>
